﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.IO;
using System.Data;

namespace SalaryChannel.Web
{
    [ServiceContract(Namespace = "http://salary.yangchao.ts.gs.chinamobile/")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class InforService : ServiceAuthRoot
    {
        [OperationContract]
        public List<VW_ValidTask> GetValidTask(string TaskName)
        {
            try
            {
                Authoricate();
                using (In_DataClassesDataContext context = new In_DataClassesDataContext())
                {
                    var query = from q in context.VW_ValidTask
                                where string.IsNullOrEmpty(TaskName) || q.Name.Contains(TaskName)
                                select q;
                    if (query.Any())
                    {
                        return query.ToList();
                    }
                    else
                    {
                        return new List<VW_ValidTask>();
                    }
                }
            }
            catch (Exception ex)
            {

                LogManager.Write("InforService.GetValidTask:" + ex.Message);
                return null;
            }
        }

        // 在此处添加更多操作并使用 [OperationContract] 标记它们
        [OperationContract]
        public List<SP_GetColFromTableResult> GetColumnFromTableByTaskID(int TaskID)
        {
            try
            {
                Authoricate();
                using (In_DataClassesDataContext context = new In_DataClassesDataContext())
                {
                    return context.SP_GetColFromTable(TaskID).ToList();
                }
            }
            catch (Exception ex)
            {

                LogManager.Write("InforService.GetColumnFromTableByTaskID:" + ex.Message);
                return null;
            }
        }
        [OperationContract]
        public bool InsertValues(string ValueString, int TaskID, string CreateID)
        {
            try
            {
                Authoricate();
                using (In_DataClassesDataContext context = new In_DataClassesDataContext())
                {
                    return context.SP_InsertInfor(ValueString, TaskID, CreateID) == 0;
                }
            }
            catch (Exception ex)
            {

                LogManager.Write("InforService.InsertValues:" + ex.Message);
                return false;
            }
        }

        private DataTable ExportInforIntoTable(int TaskID, string OperID, string Start, string End,
            int RowCount, ref string InforName)
        {
            using (SqlConnection con = new SqlConnection(SqlString))
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandTimeout = 600;
                cmd.Connection = con;
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.CommandText = "SP_ExportInfo";
                SqlParameter ret = new SqlParameter();
                ret.ParameterName = "@taskName";
                ret.Direction = ParameterDirection.Output;
                ret.DbType = DbType.String;
                ret.Size = 200;
                cmd.Parameters.AddRange(new SqlParameter[]
                {
                    new SqlParameter("@TaskID",TaskID),
                    new SqlParameter("@Oper_ID",OperID),
                    new SqlParameter("@Start_Date",Start),
                    new SqlParameter("@End_date",End),
                    new SqlParameter("@RowCount",RowCount),
                    ret
                    
                });
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                DataTable table = new System.Data.DataTable();
                sda.Fill(table);

                InforName = ret.Value.ToString().Trim();
                return table;
            }
        }

        [OperationContract]
        public string ExportInfoIntoFile(int TaskID, string OperID, string Start, string End)
        {
            
            try
            {
                Authoricate();
                string name = string.Empty;

                DataTable table = ExportInforIntoTable(TaskID, OperID, Start, End,0, ref name);

                string filename = WriteTableIntoFile(table, name);
                return filename;

            }
            catch (Exception ex)
            {

                LogManager.Write("InforService.ExportInfoIntoFile:" + ex.Message);
                return "-1";
            }
        }

        [OperationContract]
        public XDataTable ExportInfoIntoString(int TaskID, string OperID, string Start, string End,int RowCount)
        {
            try
            {
                Authoricate();
                string name = string.Empty;
                DataTable table = ExportInforIntoTable(TaskID, OperID, Start, End, RowCount, ref name);
                table.TableName = name;
                return new XDataTable(table);
            }
            catch (Exception ex)
            {
                 LogManager.Write("InforService.ExportInfoIntoString:" + ex.Message);
                return null;
            }
        }
    }
}
